package com.laxcen.smoke.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.dao.ResourceDao;
import com.laxcen.smoke.dto.res.ResourceListRes;
import com.laxcen.smoke.service.ResourceService;
import com.laxcen.smoke.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;


@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Override
    public Response insert(String name,String type,String url,String permission,long parentid,int sort,int external,int available) {
        resourceDao.insert(name, type, url, permission, parentid, sort, external, available,new Date());
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response delete(long id) {
        resourceDao.delete(id);
        resourceDao.deleteRoleAndResource(id);
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response deletemore(String id) {
        String[] ids = id.split(",");
        for (int i = 0;i < ids.length;i++){
            resourceDao.delete(Long.valueOf(ids[i]));
            resourceDao.deleteRoleAndResource(Long.valueOf(ids[i]));
        }

        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response update(long id,String name,String type,String url,String permission,long parentid,int sort,int external,int available) {
        resourceDao.update(id, name, type, url, permission, parentid, sort, external, available,new Date());
        Response response = new Response();
        response.setOK();
        return response;
    }

    @Override
    public Response<PageInfo<ResourceListRes>> get(int pageNo, int pageSize) {
        Page page = PageHelper.startPage(pageNo, pageSize);
        List<ResourceListRes> list = resourceDao.getResourceAllList();
        Response<PageInfo<ResourceListRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }

    @Override
    public Response<PageInfo<ResourceListRes>> getByTab(String name, int pageNo, int pageSize) {
        Page page = PageHelper.startPage(pageNo, pageSize);
        List<ResourceListRes> list = new LinkedList<>();
        if ("".equals(name)||null==name){
            list = resourceDao.getResourceAllList();
        }else {
             list = resourceDao.getResourceListByName(name);
        }
        Response<PageInfo<ResourceListRes>> response = new Response<>();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        response.setOK();
        response.setData(pageInfo);
        return response;
    }
}
